package com.eveinfo.widget;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
import android.util.Log;

public class WidgetProvider extends AppWidgetProvider {

	private static final String TAG = "WidgetProvider";
	private static final int UPDATE_RATE_SECONDS = 600;

	static final String URI_SCHEME = "cpu_widget";

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.d(TAG, "onEnabled");
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Log.d(TAG, "onUpdate");
		Log.d(TAG, "lenght " + appWidgetIds.length);
		for (@SuppressWarnings("unused")
		int appWidgetId : appWidgetIds) {
			Intent intent = new Intent(context, WidgetService.class);
			context.startService(intent);
		}
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(TAG, "onReceive");

		final String action = intent.getAction();
		System.out.println(action);

		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {

			if (!URI_SCHEME.equals(intent.getScheme())) {

				final int[] appWidgetIds = intent.getExtras().getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);

				for (int appWidgetId : appWidgetIds) {
					if (UPDATE_RATE_SECONDS != -1) {
						Log.i("", "Starting recurring alarm for id " + appWidgetId);
						Intent widgetUpdate = new Intent();
						widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
						widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });
						widgetUpdate.setData(Uri.withAppendedPath(Uri
								.parse(WidgetProvider.URI_SCHEME + "://widget/id/"), String.valueOf(appWidgetId)));
						PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate,
								PendingIntent.FLAG_UPDATE_CURRENT);
						AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
						alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
								UPDATE_RATE_SECONDS * 1000, newPending);
					}
				}
			}
			super.onReceive(context, intent);
		} else {
			super.onReceive(context, intent);
		}
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		Log.d(TAG, "onDeleted");
		for (int appWidgetId : appWidgetIds) {
			Intent widgetUpdate = new Intent();
			widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
			widgetUpdate.setData(Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"), String
					.valueOf(appWidgetId)));
			PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate,
					PendingIntent.FLAG_UPDATE_CURRENT);

			AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			alarms.cancel(newPending);
		}
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Log.d(TAG, "onDisabled");
	}
}
